// pages/exchangeDetail/exchangeDetail.js
var common = require("../../../common/common.js").common;

var WxParse = require("../../../wxParse/wxParse.js");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        alert: 0,
        isIPX: "",
        buy_count: 1
    },
    //获取产品信息
    getProInfo(gid) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Integral/integ_good/show",
            method: "post",
            data: {
                openid: openid,
                gid: gid
            },
            success: res => {
                console.log(res, "pro");
                var details = res.extra;
                var carouselList;
                if (details.carousel) {
                    carouselList = details.carousel.split(",");
                } else {
                    carouselList = [];
                }
                if (details.type == 1) {
                    that.setData({
                        details: details,
                        carouselList: carouselList,
                        detail: WxParse.wxParse("detail", "html", details.detail, that, 0)
                    });
                } else if (details.type == 3) {
                    details.sku_attribute = details.sku_attribute.map(item => {
                        item.attribute_value = this.selectSingleSku(item.attribute_value);
                        return item;
                    });
                    let sku_attribute = details.sku_attribute;
                    let sku = sku_attribute.reduce((s, v) => {
                        s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
                        return s;
                    }, "");
                    let sku_info = this.getProductBySku(details.sku, sku);
                    that.setData({
                        sku: sku,
                        details: details,
                        sku_info: sku_info,
                        carouselList: carouselList,
                        detail: WxParse.wxParse("detail", "html", details.detail, that, 0)
                    });
                }
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        console.log(options, "options");
        var that = this;
        var pid = options.pid;
        that.setData({
            pid: pid,
            domain: app.globalData.domain.slice(0, -10)
        });
        that.getProInfo(pid);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //立即兑换
    // exchangeButt() {
    //   var that=this;
    //   let sku_item =that.data.pid + ',' + "1";
    //   wx.navigateTo({
    //     url: '/integrationPackage/pages/exchange_order/exchange_order?integal_pro_item_id=' + integal_pro_item_id,
    //   })
    // },
    close_alert: function(e) {
        this.setData({
            alert: 0
        });
    },
    exchangeButt: function() {
        let that = this;
        if (that.data.details.type == 1) {
            let pathUrl;
            pathUrl = "/integrationPackage/pages/exchange_order/exchange_order?gid=" + that.data.pid;
            wx.navigateTo({
                url: pathUrl
            });
        } else if (that.data.details.type == 3) {
            var alert = that.data.alert;
            if (alert == 0) {
                this.setData({
                    alert: 1,
                    btn_true: 2
                });
            } else {
                that.buy_current_ch();
            }
        }
    },
    /** 
   * 添加 sku 属性
   */
    addSkuAttribute: function(e) {
        console.log(e);
        let key_id = parseInt(e.currentTarget.dataset.key_id);
        let value_id = parseInt(e.currentTarget.dataset.value_id);
        let a_key_index = parseInt(e.currentTarget.dataset.a_key_index);
        let a_value_index = parseInt(e.currentTarget.dataset.a_value_index);
        let sku = this.data.sku;
        sku = this.reBlanceSku(sku, key_id, value_id);
        console.log(sku);
        let attribute = this.data.details.sku_attribute;
        attribute = this.selectSku(attribute, a_key_index, a_value_index);
        let sku_info = this.getProductBySku(this.data.details.sku, sku);
        this.setData({
            sku: sku,
            ["details.sku_attribute"]: attribute,
            sku_info: sku_info
        });
    },
    /** 
   * 重平衡 sku
   * key_id: integer
   */
    reBlanceSku: function(sku, key_id, value_id) {
        let sku_lib = sku != "" ? this.splitSku(sku) : [];
        sku_lib = this.addSku(sku_lib, {
            key_id: key_id,
            value_id: value_id
        });
        sku_lib = this.sortSku(sku_lib);
        return this.buildSku(sku_lib);
    },
    /** 
   * 重建 sku
   */
    buildSku: function(sku_lib) {
        return sku_lib.map(item => {
            return item.key_id + "," + item.value_id;
        }).join("|");
    },
    /** 
   * 对选择的 sku 属性进行排序
   */
    sortSku: function(sku_lib) {
        return sku_lib.sort((pre, next) => {
            if (pre.key_id > next.key_id) {
                return 1;
            } else if (pre.key_id < next.key_id) {
                return -1;
            } else {
                return 0;
            }
        });
    },
    /** 
   * 分割现有 sku 成对象数组
   */
    splitSku: function(sku) {
        return sku.split("|").map(sku_tmp => {
            let [key, value] = sku_tmp.split(",");
            const t = {};
            t.key_id = parseInt(key);
            t.value_id = parseInt(value);
            return t;
        });
    },
    /** 
   * 添加一个 sku
   */
    addSku: function(sku_lib, sku) {
        let t = sku_lib.filter(item => {
            if (item.key_id === sku.key_id) {
                return false;
            }
            return true;
        });
        t.push(sku);
        return t;
    },
    /** 
   * 选择 sku
   */
    selectSku: function(attribues, a_index, v_index = 0) {
        if (attribues[a_index]) {
            let values = attribues[a_index].attribute_value;
            values = values.map(item => {
                item.selected = false;
                return item;
            });
            values[v_index].selected = true;
            attribues[a_index].attribute_value = values;
        }
        return attribues;
    },
    /** 
   * 选择单个 sku 属性
   */
    selectSingleSku: function(attribute_value, v_index = 0) {
        attribute_value.map(item => {
            item.selected = false;
            return item;
        });
        attribute_value[v_index].selected = true;
        return attribute_value;
    },
    /**
   * 根据 sku 获取物品
   */
    getProductBySku: function(sku_products, sku) {
        return sku_products[sku];
    },
    save_count: function(e) {
        this.setData({
            buy_count: parseInt(e.detail.value)
        });
    },
    blur: function(e) {
        let count = e.detail.value;
        if (count <= 0) {
            count = 1;
        }
        this.setData({
            buy_count: parseInt(count)
        });
    },
    reduce_count: function() {
        let buy_count = this.data.buy_count;
        if (buy_count > 1) {
            buy_count--;
        }
        this.setData({
            buy_count: buy_count
        });
    },
    add_count: function(e) {
        let that = this;
        let buy_count = this.data.buy_count;
        buy_count++;
        this.setData({
            buy_count: buy_count
        });
    },
    buy: function() {
        let that = this;
        var details = that.data.details;
        var sku_info = that.data.sku_info;
        var sku_id = sku_info.item_id;
        let pathUrl;
        pathUrl = "/integrationPackage/pages/exchange_order/exchange_order?gid=" + that.data.pid + "&product_item_id=" + sku_id;
        wx.navigateTo({
            url: pathUrl
        });
    }
});